<?php

/*
 * Парсим картинки для OpenCart.
 * 
 * eosagm@gmail.com
 * 
 * icq# 217861501
 * 
 * 
 */

class pParser {

    private $html;

    public function __construct() {
        
    }
/*
    public function __destruct() {
        $this->html->clear();
        unset($this->html);
    }
*/
    public function getImageLink($content) {


        $this->html = str_get_html($content);

        $link = array();
        $i = 0;

        $elements = $this->html->find('.b-images-item__link');

        foreach ($elements as $element) {

            $tmp = $this->getUrl($element->href);
            if (!empty($tmp)) {
                $link[$i]['href'] = $tmp;
                $i++;
            }
        }
        
        $this->html->clear();
        return $link;
    }

    private function getUrl($url) {
        $url = urldecode($url);
        $matches=array();
        preg_match("/(http:\/\/(.*?)(jpg|png|gif|jpeg|bmp))(.*?)/", $url, $matches);

        if (isset($matches[1])){
        return $matches[1];
        }
        return;
    }

    public function getFileName($url) {
        $url = urldecode($url);
        $matches=array();
        preg_match("/http:\/\/(.*)\/(.*?(jpg|png|gif|jpeg|bmp))(.*?)/", $url, $matches);
        if (isset($matches[2])){
        return $matches[2];
        }
        return;
    }

}

class pCurl {

    private $curl;

    public function __construct() {
        
    }

    public function getContent($url) {

        $this->curl = curl_init();
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt ($this->curl , CURLOPT_USERAGENT , "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11");
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->curl, CURLOPT_HEADER, 0);
        curl_setopt($this->curl, CURLOPT_HTTPGET, 1);

        $result = curl_exec($this->curl);

        curl_close($this->curl);

        if ($result === false) {

            $result = curl_error($this->curl);
        }

        return $result;
    }

    public function getImage($url, $filename) {

        //  $fp = fopen('./img/'.$filename, 'wb');
        
        $path = DIR_IMAGE.'data/';
        
        $ch = curl_init($url);

        // curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt ($ch , CURLOPT_USERAGENT , "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $rawdata = curl_exec($ch);
        curl_close($ch);

        if (file_exists($path.$filename)) :
           // unlink($filename);
            $filename = uniqid().$filename;
        endif;
        $fp = fopen($path.$filename, 'xb');
        fwrite($fp, $rawdata);
        fclose($fp);
        
        return $filename;
       
    }

}

class pFacade {

    private $pparser;
    private $pcurl;
    private $search;

    public function __construct() {

        $this->pparser = new pParser();
        $this->pcurl = new pCurl();
        $this->search = 'http://images.yandex.ru/yandsearch?text=';
    }

    public function parseYandex($url) {
        
        $url = $this->search.urlencode($url);
        $content = $this->pcurl->getContent($url);
        if (empty($content)) {
            echo "empty";
            return;
        }
        $imageLink = $this->pparser->getImageLink($content);
        return $imageLink;
    }

    public function downloadImage($url) {
        $filename = $this->pparser->getFileName($url);
        $filename = $this->pcurl->getImage($url, $filename);
        return $filename;
    }

}

?>